using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using GameHanhTrinh.HanhTrinh;

namespace GameHanhTrinh.FrameWork
{
    class CGamePlay
    {
        public static Game _game;
        public CGameScreen _currentState;
        public CGameScreen _nextState;

        private CSprite _background = null;
        private CResourceManager _RM = null;

        public CGamePlay(Game game)
        {
            _game = game;
            _RM = new CResourceManager(ResourceMgrId.RESOURCE_GAMEPLAY);
            _currentState = new CMenuState(GameStateID.STATE_MENU, this);
            //_currentState = new COptionState(GameStateID.STATE_OPTION, this);
            _currentState.TransitionOnTime = TimeSpan.FromSeconds(0.5);
            _currentState.TransitionOffTime = TimeSpan.FromSeconds(0.75);
            _nextState = _currentState;
        }

        public void Init()
        {
            _RM.Init(_game.Content);
            _background = new CSprite(ResourceId.MAINBACKGROUND, _RM.GetTexture(ResourceId.MAINBACKGROUND), new Vector2(0, 0), 1, 1, 1065, 800, 0);
            _currentState.Init();
            _currentState.TransitionOn();
        }
        
        public void Update(GameTime gameTime, CInput input)
        {
            if (_currentState == _nextState)
                _currentState.Update(gameTime, input);

            if (_currentState != _nextState)
            {
                if (!_nextState.IsInit)
                    _nextState.Init();


                if (_currentState.ScreenState == ScreenState.Active)
                    _currentState.TransitonOff();
                if (_nextState.ScreenState == ScreenState.None)
                    _nextState.TransitionOn();

                if (_currentState.ScreenState == ScreenState.Exiting && _nextState.ScreenState == ScreenState.Active)
                    _currentState = _nextState;
                _currentState.Update(gameTime, input);
                _nextState.Update(gameTime, input);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            _background.Draw(spriteBatch);
            if (_currentState == _nextState)
            {
                _currentState.Draw(spriteBatch);
            }
            else
            {
                _currentState.Draw(spriteBatch);
                _nextState.Draw(spriteBatch);
            }
        }
    }
}
